""" Debug functions. """

import warnings

__all__ = (
    'set_mode',
    'message',
    'warn'
)

_debug = False
_warn = True

def set_mode(d=True):
    """ Sets the debug mode. (True or False)
        If set to True, various messages will be printed to stdout or stderr
        when certain things happen.
        It is safe to call this before initializing Sbak.
    """
    global _debug
    _debug = bool(d)

def message(msg):
    """ Outputs a message if debug mode is active.
        It is safe to call this before initializing Sbak.
    """
    if _debug:
        print "sbak:",msg

def set_warn(d=True):
    """ Sets the warning mode for Sbak. (True of False)
    
        If True, warning messages will be displayed whenever some non-
        critical error occurs during the operation of Sbak. If False,
        Sbak issues no warnings. This does not effect Python's global
        warning settings.
        
        Warnings are on by default.
    """
    global _warn
    _warn = bool(d)

def warn(msg):
    """ Outputs a warning using Python's standard system.
        This is shown even when debugging is turned off.
    """
    if _warn:
        warnings.warn('\n**sbak warning: %s'%msg)